<template>
  <div>
    <!-- 搜索条件盒子 -->
    <div>
      <p class="pinformation" style="margin-top: 7px">搜索条件</p>
      <el-form label-width="70" ref="queryRef" :model="queryParams">
        <el-row>
          <el-col :span="3">
            <el-form-item label="方案编号：" prop="projectCode">
              <el-input maxlength="100" clearable placeholder="请输入方案编号" v-model="queryParams.projectCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="委托单位：" prop="wtCustomName" label-width="80">
              <el-input maxlength="100" clearable placeholder="请输入委托单位" v-model="queryParams.wtCustomName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="方案名称：" prop="projectName" label-width="80">
              <el-input maxlength="100" clearable placeholder="请输入方案名称" v-model="queryParams.projectName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="样品编号：" prop="sampleCode" label-width="80">
              <el-input maxlength="100" clearable placeholder="请输入样品编号" v-model="queryParams.sampleCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检测项目：" prop="jcxmName" label-width="80">
              <el-input maxlength="100" clearable placeholder="请输入检测项目" v-model="queryParams.jcxmName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="方法编号：" prop="methodCode" label-width="80">
              <el-input maxlength="100" clearable placeholder="请输入方案编号" v-model="queryParams.methodCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="交接时间：">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                start-placeholder="开始日期" end-placeholder="结束日期" style="width: 100%"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="紧急类型：" prop="degree" label-width="80">
              <el-select v-model="queryParams.degree" placeholder="请选择紧急类型" @change="handleQuery" style="width: 100%">
                <el-option v-for="dict in proxy.addListNoLimit(sys_degree)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="交接批次：" prop="withNumber" label-width="80">
              <el-input maxlength="100" clearable placeholder="请输入交接批次" v-model="queryParams.withNumber"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="检测类别：" prop="jclbId" label-width="80">
              <el-select v-model="queryParams.jclbId" placeholder="请选择检测类别" @change="handleQuery" style="width: 100%">
                <el-option v-for="dict in proxy.addListNoLimit(
                  jclbList,
                  'jclbId',
                  'jclbName'
                )" :key="dict.jclbId" :label="dict.jclbName" :value="dict.jclbId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-row style="justify-content: center">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="business resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 检测类别列表盒子 -->
    <div>
      <el-row>
        <p class="pinformation">样品列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
      </el-row>
      <el-table ref="mainTableRef" :data="projectList" v-loading="loading">
        <el-table-column label="样品编号" align="center" prop="sampleCode" width="140" />
        <el-table-column label="检测项目" align="center" prop="jcxmNames">
          <template #default="scope">
            <span v-html="proxy.getSupAndSub(scope.row.jcxmNames)"></span>
          </template>
        </el-table-column>
        <el-table-column label="方案编号" align="center" prop="projectCode" width="100" />
        <el-table-column label="方案名称" align="center" prop="projectName" width="260" />
        <el-table-column label="检测点位" align="center" prop="pointName" width="100" />
        <el-table-column label="频次" align="center" width="130">
          <template #default="scope">
            <span v-if="scope.row.sampleDay && scope.row.sampleCount">第{{ scope.row.sampleDay }}天, 第{{
              scope.row.sampleCount }}次</span>
          </template>
        </el-table-column>
        <el-table-column label="采样时间" align="center">
          <template #default="scope">
            {{ scope.row.cyDate }} &nbsp; {{ scope.row.cyTime }}
          </template>
        </el-table-column>
        <el-table-column label="保存时长" align="center" prop="saveTime" width="80" />
        <el-table-column label="保存方式" align="center" prop="saveDesc" />
        <el-table-column label="交接批次" align="center" prop="withNumber" width="80" />
        <el-table-column label="检测类别" align="center" prop="jclbName" width="120" />
        <el-table-column label="分析组" align="center" prop="userGroupName" width="80" v-if="props.isRepertory === 0" />
        <el-table-column label="领取人" align="center" prop="getUserName" width="110" v-if="props.isRepertory === 1" />
        <el-table-column label="领取时间" align="center" prop="getTime" v-if="props.isRepertory === 1" />
        <el-table-column label="化验员" align="center" prop="writeUserName" width="110" v-if="props.isRepertory === 1" />
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
  </div>
</template>

<script setup>
import { listProjct } from "@/api/sampleManage/sampleRepertory";
import { allListJclb } from "@/api/store/jclb";

const { proxy } = getCurrentInstance();
const { sys_contract_type, sys_degree } = proxy.useDict(
  "sys_contract_type",
  "sys_degree"
);

const total = ref(0);
const projectList = ref([]);
const dateRange = ref([]);
const loading = ref(true);
const jclbList = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    degree: -1,
    jclbId: -1,
    getId: props.isRepertory,
  },
});

const { queryParams } = toRefs(data);

//定义参数 已经选中的ITEMID
const props = defineProps({
  isRepertory: {
    type: Number, //参数类型
  },
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 74 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});

onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function getList() {
  loading.value = true;
  listProjct(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      projectList.value = res.rows;
      total.value = res.total;
      loading.value = false;
    }
  );
}

function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  getList();
}

// 获取检测类别列表数据
function getJclbList() {
  allListJclb().then((res) => {
    jclbList.value = res.data;
  });
}

getJclbList();
getList();
</script>
